﻿//Eggtimer by Thomas Arvidsson, 2011-12-27


using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Shell;

namespace EggTimer.EggClass
{
    public class Egg
    {
        #region Constants
        public const double _eggSizeSmall = 12.5;
        public const double _eggSizeMedium = 14.4;
        public const double _eggSizeLarge = 16.0;
        public const double _eggSizeXL = 16.8;

        public const double _eggInFridge = 5.0;
        public const double _eggInRoom = 22.0;

        public const double _eggTempSoft = 66.0;
        public const double _eggTempMedium = 72.5;
        public const double _eggTempFirm = 80.0;
        public const double _eggTempExtraFirm = 84.0;
        #endregion Constants


        #region Fields and propertys
        private double _boilingWaterTemp = 99.6;
        public double BoilingWaterTemp
        {
            get { return _boilingWaterTemp; }
            set { _boilingWaterTemp = value; }
        }

        private double _eggSizeToCount;
        public double EggSizeToCount
        {
            get { return _eggSizeToCount; }
            set
            {
                _eggSizeToCount = value;
                //SecToBoil = GetTimerSeconds();
            }
        }

        private double _eggTempBeforeCooking;
        public double EggTempBeforeCooking
        {
            get { return _eggTempBeforeCooking; }
            set
            {
                _eggTempBeforeCooking = value;
                //SecToBoil = GetTimerSeconds();
            }
        }

        private double _eggTempDone;
        public double EggTempDone
        {
            get { return _eggTempDone; }
            set
            {
                _eggTempDone = value;
                //SecToBoil = GetTimerSeconds();
            }
        }

        private double secToBoil;
        public double SecToBoil
        {
            get { return secToBoil; }
            set
            {
                secToBoil = Math.Round(value, 0);
                //txtBlockTimer.Text = secToBoil.ToString();
                if (SecToBoil < 2.0)
                {
                    //Turn on Idlemode if counter is almost zero
                    PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
                }
            }
        }
        #endregion Fields and propertys



        /// <summary>
        /// Calculates the cooking time of the egg
        /// </summary>
        /// <returns></returns>
        public double GetTimerSeconds(double eggSizeToCount, double boilingWaterTemp, double eggTempBeforeCooking, double eggTempDone)
        {
            double seconds = Math.Pow(eggSizeToCount, 2) * Math.Log(2 * ((boilingWaterTemp - eggTempBeforeCooking) / (boilingWaterTemp - eggTempDone)));
            return seconds;
        }

        /// <summary>
        /// Calculates the cooking time of the egg. Original formula t = circumference ^2 ln(2*((Watertemp - EggTempBeforeCooking) / (WaterTemp - EggTempDone)))
        /// 
        /// </summary>
        /// <returns></returns>
        public double GetTimerSeconds()
        {
            double seconds = Math.Pow(this.EggSizeToCount, 2) * Math.Log(2 * ((this.BoilingWaterTemp - this.EggTempBeforeCooking) / (this.BoilingWaterTemp - this.EggTempDone)));
            return seconds;
        }

        /// <summary>
        /// Returns a string of text representing the timer value
        /// </summary>
        /// <returns></returns>
        public string GetTimerValue()
        {
            string timerValue = "";
            decimal minutes = (decimal)this.SecToBoil / 60;
            minutes = decimal.Truncate(minutes);

            decimal seconds = (decimal)this.SecToBoil % 60;
            if (seconds < 10 && seconds >= 0)
            {
                timerValue = minutes.ToString() + ":" + "0" + seconds.ToString();
            }
            else
            {
                timerValue = minutes.ToString() + ":" + seconds.ToString();
            }

            return timerValue;
        }

        // ////////Lägg till metod för att returnera äggets temperatur för tillfället.

        /// <summary>
        /// Returna a value representing the temp of the yolk at this moment
        /// </summary>
        /// <returns></returns>
        public double GetYolkTemp()
        {
            throw new NotImplementedException();
        }
    }
}
